//
//  MyAppointmentFormViewController.m
//  iModern
//
//  Created by 立雄 王 on 15/7/6.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//
//
#import "MyAppointmentFormViewController.h"
#import "XLFormViewController+Helper.h"
#import "NSDate+Local.h"
#import "AppDelegate.h"
#import "iModern-Swift.h"
#import "WebchatDelegate.h"

@interface MyAppointmentFormViewController () <UIAlertViewDelegate>
{
    AppDelegate           *appDelegate;
    UIAlertView           *tipAlertView;
    PayTransactionManager *payManager;
}
@end

NSString *const payButton          = @"pay";
NSString *const compliteButton     = @"compliteButton";
NSString *const Button             = @"button";
NSString *const ChooseButton       = @"choose";
NSString *const sButtonLeftAligned = @"buttonLeftAligned";
NSString *const mButtonLeftAligned = @"buttonLeftAligned";

double tip = 0.0;

@implementation MyAppointmentFormViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage         *image          = [UIImage imageNamed:@"Back-Small.png"];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToPreviousView)];
    self.navigationItem.leftBarButtonItem = backButtonItem;

    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    NSDictionary            *data = self.contact;
    XLFormDescriptor        *form;
    XLFormSectionDescriptor *section;
    XLFormRowDescriptor     *row;

    form = [XLFormDescriptor formDescriptor];

    section = [XLFormSectionDescriptor formSectionWithTitle:@"卖 家"];
    [form addFormSection:section];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"alias" rowType:XLFormRowDescriptorTypeText];
    row.title = @"客户别名:";
    row.value = self.contact[@"advertising"][@"contact"][@"alias"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"fullName" rowType:XLFormRowDescriptorTypeText];
    row.title = @"客户姓名:";
    row.value = self.contact[@"advertising"][@"contact"][@"fullName"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"cellphone" rowType:XLFormRowDescriptorTypeText];
    row.title = @"客户电话";
    row.value = self.contact[@"advertising"][@"contact"][@"cellphone"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"email" rowType:XLFormRowDescriptorTypeText];
    row.title = @"客户电邮:";
    row.value = self.contact[@"advertising"][@"contact"][@"email"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"address" rowType:XLFormRowDescriptorTypeText];
    row.title = @"客户地址:";
    row.value = self.contact[@"advertising"][@"contact"][@"address"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    section = [XLFormSectionDescriptor formSectionWithTitle:@"商 品"];
    [form addFormSection:section];

    XLFormRowDescriptor *sbuttonLeftAlignedRow = [XLFormRowDescriptor formRowDescriptorWithTag:sButtonLeftAligned rowType:XLFormRowDescriptorTypeButton title:[NSString stringWithFormat:@"商品名称: %@", data[@"advertising"][@"summary"]]];
    [sbuttonLeftAlignedRow.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
    sbuttonLeftAlignedRow.action.formSelector = @selector(buttonLeftAlignedRow);
    [section addFormRow:sbuttonLeftAlignedRow];

    XLFormRowDescriptor *mbuttonLeftAlignedRow = [XLFormRowDescriptor formRowDescriptorWithTag:mButtonLeftAligned rowType:XLFormRowDescriptorTypeButton title:[NSString stringWithFormat:@"商品说明: %@", data[@"advertising"][@"message"]]];
    [mbuttonLeftAlignedRow.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
    mbuttonLeftAlignedRow.action.formSelector = @selector(buttonLeftAlignedRow);
    [section addFormRow:mbuttonLeftAlignedRow];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"price" rowType:XLFormRowDescriptorTypeText];
    row.title = @"价格:";
    row.value = self.contact[@"price"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    section = [XLFormSectionDescriptor formSectionWithTitle:@"预 约"];
    [form addFormSection:section];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"id" rowType:XLFormRowDescriptorTypeText];
    row.title = @"订单号:";
    row.value = self.contact[@"id"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"advertising.message" rowType:XLFormRowDescriptorTypeText];
    row.title = @"备注:";
    row.value = self.contact[@"message"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startTime" rowType:XLFormRowDescriptorTypeDateTime];
    row.title = @"时间:";
    if (![self.contact[@"startTime"] isEqual:[NSNull null]])
    {
        row.value = [NSDate dateFromJSONString:self.contact[@"startTime"]];
    }
    else
    {
        row.value = [NSDate new];
    }
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"status" rowType:XLFormRowDescriptorTypeText];
    row.title = @"状态:";
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    section = [XLFormSectionDescriptor formSectionWithTitle:@"操 作"];
    [form addFormSection:section];
    payManager = [PayTransactionManager new];

    XLFormRowDescriptor *statusRow = [form formRowWithTag:@"status"];

    if ([self.contact[@"status"] isEqualToString:@"Not Started"])
    {
        XLFormRowDescriptor *buttonRow = [XLFormRowDescriptor formRowDescriptorWithTag:Button rowType:XLFormRowDescriptorTypeButton title:@"取消订单"];
        buttonRow.action.formSelector = @selector(didTouchButton:);
        [section addFormRow:buttonRow];

        statusRow.value = @"订单还未确认";
    }

    if ([self.contact[@"status"] isEqualToString:@"Started"])
    {
        ResultSet *rs = [payManager queryLastTransactionRecord:[self.contact[@"id"] stringValue]];
        if (rs.statusCode != 400)
        {
            if ([rs.data[@"status"] isEqualToString:@"Pending"])
            {
                statusRow.value = @"订单正在确认支付中.";
            }
            else if ([rs.data[@"status"] isEqualToString:@"Completed"])
            {
                statusRow.value = @"订单支付结果已确认.";
            }
            else
            {
                XLFormRowDescriptor *payButtonChooseRow = [XLFormRowDescriptor formRowDescriptorWithTag:payButton rowType:XLFormRowDescriptorTypeButton title:@"完成并付款"];
                payButtonChooseRow.action.formSelector = @selector(didTouchPayButton:);
                [section addFormRow:payButtonChooseRow];

                XLFormRowDescriptor *compliteButtonChooseRow = [XLFormRowDescriptor formRowDescriptorWithTag:payButton rowType:XLFormRowDescriptorTypeButton title:@"完成"];
                compliteButtonChooseRow.action.formSelector = @selector(didTouchCompliteButton:);
                [section addFormRow:compliteButtonChooseRow];

                statusRow.value = @"订单已经被确认";
            }
        }
        else
        {
            XLFormRowDescriptor *payButtonChooseRow = [XLFormRowDescriptor formRowDescriptorWithTag:payButton rowType:XLFormRowDescriptorTypeButton title:@"完成并付款"];
            payButtonChooseRow.action.formSelector = @selector(didTouchPayButton:);
            [section addFormRow:payButtonChooseRow];

            XLFormRowDescriptor *compliteButtonChooseRow = [XLFormRowDescriptor formRowDescriptorWithTag:payButton rowType:XLFormRowDescriptorTypeButton title:@"完成"];
            compliteButtonChooseRow.action.formSelector = @selector(didTouchCompliteButton:);
            [section addFormRow:compliteButtonChooseRow];

            statusRow.value = @"订单已经被确认";
        }
    }

    if ([self.contact[@"status"] isEqualToString:@"Cancelled"])
    {
        statusRow.value = @"订单已取消";
    }

    if ([self.contact[@"status"] isEqualToString:@"Completed"])
    {
        //        XLFormRowDescriptor *buttonChooseRow = [XLFormRowDescriptor formRowDescriptorWithTag:ChooseButton rowType:XLFormRowDescriptorTypeButton title:@"评 价"];
        //        buttonChooseRow.action.formSelector = @selector(didTouchChooseButton:);
        //        [section addFormRow:buttonChooseRow];

        statusRow.value = @"订单已完成";
    }

    self.form = form;
}

- (void)didTouchPayButton:(XLFormRowDescriptor *)sender
{
    tipAlertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"商品价格: %@元", self.contact[@"price"]] message:@"请在下面的输入框输入小费" delegate:self cancelButtonTitle:@"取消付款" otherButtonTitles:@"确定付款", nil];
    UITextField *txtField = [[UITextField alloc] init];
    txtField.delegate = self;

    txtField.backgroundColor = [UIColor whiteColor];
    txtField.frame           = CGRectMake(tipAlertView.center.x + 65, tipAlertView.center.y + 48, 150, 23);

    txtField.keyboardAppearance = UIKeyboardAppearanceDefault;

    [tipAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];

    [[tipAlertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
    [[tipAlertView textFieldAtIndex:0] setPlaceholder:@"请输入小费"];
    [[tipAlertView textFieldAtIndex:0] setText:@"0"];
    [tipAlertView addSubview:txtField];

    [tipAlertView show];
    [self deselectFormRow:sender];
}

- (void)didTouchCompliteButton:(XLFormRowDescriptor *)sender
{
    //调用直接完单接口
    ResultSet *rs = [payManager completeAppointment:[self.contact[@"id"] intValue]];
    if (rs.statusCode == 200)
    {
    }
    [appDelegate.navigationController popViewControllerAnimated:YES];
}

- (void)didTouchChooseButton:(XLFormRowDescriptor *)sender
{
}

- (void)didTouchButton:(XLFormRowDescriptor *)sender
{
    ResultSet *rs = [payManager cancelAppointment:[self.contact[@"id"] intValue]];
    if (rs.statusCode == 200)
    {
    }
    [appDelegate.navigationController popViewControllerAnimated:YES];
    [self deselectFormRow:sender];
}

- (void)buttonLeftAlignedRow
{
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField *tf = [alertView textFieldAtIndex:0];
    if ([tf.text isEqualToString:@""])
    {
        tip = 0.0;
    }
    else
    {
        tip = [tf.text doubleValue];
    }

    if (buttonIndex == 1)
    {
        WebchatDelegate *webChatDelegate = [[WebchatDelegate alloc] init];
        [webChatDelegate weixinPay:[self.contact[@"id"] stringValue] tip:tip];
    }
}

- (void)backToPreviousView
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
